package com.gowants.pems.util;

import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import org.springframework.web.multipart.MultipartFile;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;

public class QrReader {

	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static Result read(MultipartFile file) 
	{
		Result result = null;
		try 
		{

			FileInputStream in = (FileInputStream) file.getInputStream();
			BufferedImage image = javax.imageio.ImageIO.read(in);
            
			BinaryBitmap bb = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
			
			HashMap map = new HashMap();
			map.put(EncodeHintType.CHARACTER_SET, "utf-8");
			
			MultiFormatReader reader = new MultiFormatReader();//需要详细了解MultiFormatReader的小伙伴可以点我一下官方去看文档
			result = reader.decode(bb, map);

		} 
		catch (NotFoundException e) 
		{
			e.printStackTrace();
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		catch(Exception e) 
		{
			e.printStackTrace();
		}
		
		return result;
	}
	
}
